<?php
    /**
    * DB.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    require_once 'MDB2.php';
    
    /**
    * Base class for objects that require database access.
    * @package tman
    */
    abstract class AT_Object_DB extends AT_Object
    {
        /**
        * The database link.
        * @var MDB2_Driver_Common
        */
        protected $mdb2;
        
        /**
        * The constructor sets up the database connection using a singleton
        * pattern.
        */
        public function __construct()
        {
            parent::__construct();
            
            static $con = NULL;
            if ( $con == NULL )
            {
                $con = MDB2::singleton(AT_DSN);
                if ( PEAR::isError($con) )
                {
                    $msg = 'Failed to connect to database: '.$con->getMessage();
                    $this->reportError($msg, PEAR_LOG_ALERT);
                }
                $con->setFetchMode(MDB2_FETCHMODE_ASSOC);
            }
            $this->mdb2 = $con;
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
